﻿using HandyControl.Controls;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UI.View.Chaobiao.Part
{
    /// <summary>
    /// WaterEdit.xaml 的交互逻辑
    /// </summary>
    public partial class WaterEdit : UserControl
    {
        ViewModel.ChaobiaoViewModel vm;


        RestSharp.RestClient client = new RestSharp.RestClient(new Uri(Comm.TempData.BaseUrl));
        public WaterEdit()
        {
            InitializeComponent();
            vm = DataContext as ViewModel.ChaobiaoViewModel;

            vm.CurrWater = new Model.F_Water() {  MeterType=vm.MeterType};
            vm.ListLayers = new List<int>();
            LoadData();

            dtp_CurrTime.SelectedDateTime = DateTime.Now;
        }


       private void LoadData()
        {
            var req = new RestRequest("building", Method.GET);
            var rsp = client.Get<ListDataResponse<Model.Building>>(req);

            if (rsp.IsSuccessful)
            {
                vm.ListBuilds = rsp.Data.data;
            }


            req = new RestRequest("room", Method.GET);
            var  rsproom = client.Get<ListDataResponse<Model.Rooms>>(req);

            if (rsproom.IsSuccessful)
            {
                vm.ListRooms = rsproom.Data.data;
            }
           
           


        }


        private void LoadRooms()
        {
           var  req = new RestRequest("room", Method.GET);
            var rsproom = client.Get<ListDataResponse<Model.Rooms>>(req);

            if (rsproom.IsSuccessful)
            {
                vm.ListRooms = rsproom.Data.data;
            }

        }
        private void Btn_Cancle_Click(object sender, RoutedEventArgs e)
        {
            vm.Dialog.Close();
            
        }

        private void Btn_Save_Click(object sender, RoutedEventArgs e)
        {


            if (vm.CurrWater.RoomId == null)
            {
                HandyControl.Controls.Growl.WarningGlobal("请选择房屋！");
                return;
            }



            if (vm.CurrWater.PreDegrees==null)
            {
                HandyControl.Controls.Growl.WarningGlobal("请输入上期读数");
                return;
            }
            if (vm.CurrWater.CurDegrees == null)
            {
                HandyControl.Controls.Growl.WarningGlobal("请输入本期读数");
                return;
            }
            if (vm.CurrWater.PreReadTime == null)
            {
                HandyControl.Controls.Growl.WarningGlobal("请输入上期抄表时间");
                return;
            }
            if (vm.CurrWater.CurReadTime == null)
            {
                HandyControl.Controls.Growl.WarningGlobal("请输入本期抄表时间");
                return;
            }

            if(vm.CurrWater.CurReadTime<=vm.CurrWater.PreReadTime)
            {
                HandyControl.Controls.Growl.WarningGlobal("本次抄表时间应晚于上次抄表时间！");
                return;
            }

            if(vm.CurrWater.CurDegrees<vm.CurrWater.PreDegrees)
            {
                HandyControl.Controls.Growl.WarningGlobal("本次度数不能小于上次读数！");
                return;
            }
            try
            {
                //RestRequest request = new RestSharp.RestRequest("water", RestSharp.Method.POST);
                //request.AddJsonBody(vm.CurrWater);
                //var resp = client.Post<SingleDataResponse<int>>(request);

                decimal? price = 0; 
                price = context.DB.Queryable<Model.Costs>().Where(c => c.Name == vm.MeterType).First().Price;
                vm.CurrWater.Price = (decimal)price;
                var ret =context.DB.Insertable<Model.F_Water>(vm.CurrWater).ExecuteCommand();

                if(ret>0)
                {
                    Growl.SuccessGlobal("保存成功！");
                    vm.Dialog.Close();
                    vm.LoadWater(vm.MeterType);
                }
                else
                {
                    Growl.WarningGlobal("保存失败！" );
                }
            }
            catch (Exception ex)
            {

                HandyControl.Controls.Growl.FatalGlobal(ex.Message);
            }
            
            
        }

        private void ComboBox_DropDownOpened(object sender, EventArgs e)
        {
            if(vm.SelectedBuild!=null)
            {
                for (int i =1; i <= vm.SelectedBuild.LayerCount; i++)
                {
                    vm.ListLayers.Add(i);
                    //
                    cmb_Layer.ItemsSource = vm.ListLayers;
                    
                }

            }
            
        }

        private void ComboBox_DropDownOpened_1(object sender, EventArgs e)
        {
            if(vm.SelectedBuild !=null)
            {
                LoadRooms();
                vm.ListRooms = vm.ListRooms.Where(r=>(int)r.BuildingId == vm.SelectedBuild.Id).ToList();
            }
        }
        Model.DataContext context = new Model.DataContext();

        private void ComboBox_DropDownClosed(object sender, EventArgs e)
        {

            try
            {
                if (cmb_SelectRoom.SelectedValue != null)
                {
                    var roomId = (int)cmb_SelectRoom.SelectedValue;

                     

                  
                   var room = context.DB.Queryable<Model.Rooms>().Where(r => r.Id == roomId).First();
                    if(room!=null)
                    {
                        vm.CurrWater.BuildId = room.BuildingId;
                        vm.CurrWater.BuildNo = room.BuilingNo;
                        vm.CurrWater.RoomId = room.Id;
                        vm.CurrWater.RoomNo = room.No;
                        vm.CurrWater.Owner = room.OwnerName;
                    }

                    var lastWater = context.DB.Queryable<Model.F_Water>().Where(r => r.RoomId == roomId && r.MeterType == vm.MeterType).OrderBy(w => w.CurReadTime, SqlSugar.OrderByType.Desc).First();
                    if (lastWater != null)
                    {
                        vm.CurrWater.PreDegrees = lastWater.CurDegrees;
                        vm.CurrWater.PreReadTime = lastWater.CurReadTime;
                    }
                    else
                    {
                        vm.CurrWater.PreReadTime = DateTime.Now.AddYears(-1);
                    }

                }
            }
            catch (Exception ex)
            {

                HandyControl.Controls.Growl.FatalGlobal( ex.Message);
            }
           
        }
    }
}
